package com.hss01248.dialog.view;

import ohos.agp.components.*;
import ohos.agp.utils.LayoutAlignment;
import ohos.app.Context;
import com.hss01248.dialog.ResourceTable;
import com.hss01248.dialog.ScreenUtil;
import com.hss01248.dialog.Tool;
import com.hss01248.dialog.adapter.SuperLvHolder;
import com.hss01248.dialog.config.ConfigBean;

/**
 * Created by huangshuisheng on 2018/4/18.
 */

public class AdXHolder extends SuperLvHolder<ConfigBean> {
    private Image ivClose;
    private DependentLayout ivCloseContainer;
    private StackLayout adContainer;
    public AdXHolder(Context context) {
        super(context);
    }

    @Override
    protected void findViews() {
        ivClose = (Image)rootView.findComponentById(ResourceTable.Id_iv_close_ad_real);
        ivCloseContainer = (DependentLayout)rootView.findComponentById(ResourceTable.Id_rl_close_ad_container);
        adContainer = (StackLayout)rootView.findComponentById(ResourceTable.Id_fl_ad_container);
    }

    @Override
    protected int setLayoutRes() {
        return ResourceTable.Layout_dialog_ad_x;
    }

    @Override
    public void assingDatasAndEvents(Context context, final ConfigBean bean) {
        Component customView = bean.customView;
        if(customView == null && bean.customContentHolder!=null){
            customView = bean.customContentHolder.rootView;
        }
        if(customView ==null){
            return;
        }
        adContainer.addComponent(customView);
        resetCloseBtnPosition(bean);

        ivCloseContainer.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component v) {
                Tool.dismiss(bean);
                bean.listener.onFirst();
            }
        });
    }

    /**
     * 根据配置的信息重新设置关闭按钮
     * @param bean
     */
    private void resetCloseBtnPosition(ConfigBean bean) {
        if(bean.xResId != 0){
            ivClose.setPixelMap(bean.xResId);
        }
        if(bean.xWidth>0){
            DependentLayout.LayoutConfig params = (DependentLayout.LayoutConfig) ivClose.getLayoutConfig();
            params.width = ScreenUtil.dip2px(bean.xWidth);
            params.height = params.width;
            ivClose.setLayoutConfig(params);

        }

        DependentLayout.LayoutConfig params = (DependentLayout.LayoutConfig) ivCloseContainer.getLayoutConfig();
        //上方三个位置
        if(bean.xGravity == (LayoutAlignment.TOP|LayoutAlignment.RIGHT)){
            DependentLayout.LayoutConfig tmpConfig = (DependentLayout.LayoutConfig)adContainer.getLayoutConfig();
            tmpConfig.addRule(DependentLayout.LayoutConfig.BELOW, ResourceTable.Id_rl_close_ad_container);
            adContainer.setLayoutConfig(tmpConfig);
            int margin = ScreenUtil.dip2px(bean.xMarginTB);
            params.setMarginsTopAndBottom(margin, margin);
            if(bean.xMarginLR >0){
                margin = ScreenUtil.dip2px(bean.xMarginLR);
                params.setMarginsLeftAndRight(margin, margin);
            }

            //对齐按钮的位置
            params.addRule(DependentLayout.LayoutConfig.ALIGN_RIGHT,ResourceTable.Id_fl_ad_container);
            //按钮内部图标也调整为右对齐
            ((DependentLayout.LayoutConfig)ivClose.getLayoutConfig()).addRule(DependentLayout.LayoutConfig.ALIGN_PARENT_RIGHT);
            ((DependentLayout.LayoutConfig)ivClose.getLayoutConfig()).addRule(DependentLayout.LayoutConfig.ALIGN_PARENT_BOTTOM);

        }else if(bean.xGravity == (LayoutAlignment.TOP|LayoutAlignment.HORIZONTAL_CENTER)){
            params.addRule(DependentLayout.LayoutConfig.HORIZONTAL_CENTER);
            DependentLayout.LayoutConfig tmpConfig = (DependentLayout.LayoutConfig)adContainer.getLayoutConfig();
            tmpConfig.addRule(DependentLayout.LayoutConfig.BELOW, ResourceTable.Id_rl_close_ad_container);
            adContainer.setLayoutConfig(tmpConfig);
            int margin = ScreenUtil.dip2px(bean.xMarginTB);
            params.setMarginsTopAndBottom(margin, margin);

            ((DependentLayout.LayoutConfig)ivClose.getLayoutConfig()).addRule(DependentLayout.LayoutConfig.ALIGN_PARENT_BOTTOM);
        }else if(bean.xGravity == (LayoutAlignment.TOP|LayoutAlignment.LEFT)){
            DependentLayout.LayoutConfig tmpConfig = (DependentLayout.LayoutConfig)adContainer.getLayoutConfig();
            tmpConfig.addRule(DependentLayout.LayoutConfig.BELOW, ResourceTable.Id_rl_close_ad_container);
            adContainer.setLayoutConfig(tmpConfig);
            int margin = ScreenUtil.dip2px(bean.xMarginTB);
            params.setMarginsTopAndBottom(margin, margin);
            if(bean.xMarginLR >0){
                margin = ScreenUtil.dip2px(bean.xMarginLR);
                params.setMarginsLeftAndRight(margin, margin);
            }

            params.addRule(DependentLayout.LayoutConfig.ALIGN_LEFT, ResourceTable.Id_fl_ad_container);
            ((DependentLayout.LayoutConfig)ivClose.getLayoutConfig()).addRule(DependentLayout.LayoutConfig.ALIGN_PARENT_LEFT);
            ((DependentLayout.LayoutConfig)ivClose.getLayoutConfig()).addRule(DependentLayout.LayoutConfig.ALIGN_PARENT_BOTTOM);
            //下方三个位置
        }else if(bean.xGravity == (LayoutAlignment.BOTTOM|LayoutAlignment.RIGHT)){
            params.addRule(DependentLayout.LayoutConfig.BELOW, ResourceTable.Id_fl_ad_container);
            int margin = ScreenUtil.dip2px(bean.xMarginTB);
            params.setMarginsTopAndBottom(margin, margin);
            if(bean.xMarginLR >0){
                margin = ScreenUtil.dip2px(bean.xMarginLR);
                params.setMarginsLeftAndRight(margin, margin);
            }
            params.addRule(DependentLayout.LayoutConfig.ALIGN_RIGHT, ResourceTable.Id_fl_ad_container);
            ((DependentLayout.LayoutConfig)ivClose.getLayoutConfig()).addRule(DependentLayout.LayoutConfig.ALIGN_PARENT_RIGHT);
            ((DependentLayout.LayoutConfig)ivClose.getLayoutConfig()).addRule(DependentLayout.LayoutConfig.ALIGN_PARENT_TOP);
        }else if(bean.xGravity == (LayoutAlignment.BOTTOM|LayoutAlignment.HORIZONTAL_CENTER)){
            params.addRule(DependentLayout.LayoutConfig.HORIZONTAL_CENTER);
            params.addRule(DependentLayout.LayoutConfig.BELOW,ResourceTable.Id_fl_ad_container);
            int margin = ScreenUtil.dip2px(bean.xMarginTB);
            params.setMarginsTopAndBottom(margin, margin);

            ((DependentLayout.LayoutConfig)ivClose.getLayoutConfig()).addRule(DependentLayout.LayoutConfig.ALIGN_PARENT_TOP);
        }else if(bean.xGravity == (LayoutAlignment.BOTTOM|LayoutAlignment.LEFT)){
            if(bean.xMarginLR >0){
                int margin = ScreenUtil.dip2px(bean.xMarginLR);
                params.setMarginsLeftAndRight(margin, margin);
            }
            params.addRule(DependentLayout.LayoutConfig.BELOW, ResourceTable.Id_fl_ad_container);
            params.setMarginTop(ScreenUtil.dip2px(bean.xMarginTB));
            params.addRule(DependentLayout.LayoutConfig.ALIGN_LEFT,ResourceTable.Id_fl_ad_container);
            ((DependentLayout.LayoutConfig)ivClose.getLayoutConfig()).addRule(DependentLayout.LayoutConfig.ALIGN_PARENT_LEFT);
            ((DependentLayout.LayoutConfig)ivClose.getLayoutConfig()).addRule(DependentLayout.LayoutConfig.ALIGN_PARENT_TOP);
        }else {
            //默认是底部居中
            params.addRule(DependentLayout.LayoutConfig.HORIZONTAL_CENTER);
            params.addRule(DependentLayout.LayoutConfig.BELOW, ResourceTable.Id_fl_ad_container);
            int margin = ScreenUtil.dip2px(bean.xMarginTB);
            params.setMarginsTopAndBottom(margin, margin);
            ((DependentLayout.LayoutConfig)ivClose.getLayoutConfig()).addRule(DependentLayout.LayoutConfig.ALIGN_PARENT_TOP);
        }
        ivCloseContainer.setLayoutConfig(params);
    }
}
